var operator = {
    init: function() {
        var that = this;
        that.getList();
         //close
        $("body").on('click', '#noticeClose,#noticeCancel', function() {
            $("#issueTmp").remove();
        });
    },
    getList: function() {
        var that = this,
            token = $.cookie('token');
        $.ajaxSettings.global = false;
        $.ajaxSend('api/BidBase/GetBidList?token=' + token, {

        }, function(redata) {
            if (redata.recode == '0000') {
                var compiled = _.template($("#listTpl").html());
                var odata = redata.mod;
                // alert(that.format(odata[0].start_bid_time))
                for (var i = 0, length = odata.length; i < length; i++) {
                    odata[i].start_bid_time = that.format(odata[i].start_bid_time);
                    odata[i].end_bid_time = that.format(odata[i].end_bid_time);
                }
                $("#listCon").html(compiled(redata.mod));
            }
        });

        $.ajaxSettings.global = true;

        that.selBid();
        that.abolish();
        that.getAnnounce();
        that.getWorklist();

         $("#Nmore").on("click",function(){
                    var type = $(this).attr("type")
                    $.cookie('Busines_List_index', type);
                    
                    window.location.href="/#Business_List";
        })

         $("#opAddBtn").on("click",function(){
                    var type = $(this).attr("type")
                    $.cookie('Standard_List_index', type);
                    
                    window.location.href="/#Standard_List";
        })
    },
    getAnnounce:function(){
        $.ajax({
                url: COMMONURL + "api/CompanyNotice/GetPageList?token=" + $.cookie('token'),
                type: "post",
                data: { 
                    page: 1,
                    pageSize: 10
                },
                dataType: "json"
            }).done(function(redata) {
                if (redata.recode == '0000') {
                    var compiled = _.template($("#AnnounceTmp").html());
                     $("#announce").html(compiled(redata.mod.Items));
                
                     $(".jLook").click(function(){

                        var $noticeIssue = $("#noticeIssue"),
                            $issueTpl = $("#issueTpl");
                        var winHeight = $(document).height();
                        var id = $(this).attr("data-id");
                     $.ajax({
                            url: COMMONURL + "api/notice/get?token=" + $.cookie('token'),
                            type: "get",
                            data: {
                                id: id
                               
                            },
                            dataType: "json"
                        }).done(function(redata) {
                            if (redata.recode == '0000') {
                                var compiled = _.template($issueTpl.html());
                                 $('body').append(compiled(redata.mod));
                                $("#issueTmp #issueMask").css('height', winHeight);
                            }else{
                                 alert(data.resmessage);
                            }
                        });
                    
                })



                }

            });
    },
    getWorklist:function(){
        $.ajax({
                url: COMMONURL + "api/BidBase/GetIndexBidCount?token=" + $.cookie('token'),
                type: "post",
                data: {
                   
                },
                dataType: "json"
            }).done(function(redata) {
                if (redata.recode == '0000') {
                    var data = redata.mod;
                    $("#workLe1").html(data.split("|")[0]);
                    $("#workLe2").html(data.split("|")[1]);
                    $("#workLe3").html(data.split("|")[2]);
                    $("#workLe4").html(data.split("|")[3]);
                }
            });
    },
    format: function(nS) {
      //return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
             // return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17);
                if(!nS) return null;
            var date = new Date(nS*1000);
            var year = date.getFullYear();
            var month = date.getMonth()+1;
            var d = date.getDate();
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            return year+'-'+month+'-'+d+' '+hours+':'+minutes;
    },
    selBid: function() {
    	
    	  //选定
                $('#selModelCon').on('click', '[data-name="selBid"]', function() {
                    var bid_wl_id = $(this).attr('data-itemid');
                    var bid_id = $(this).attr('data-mybid');
                    $.ajax({
                        url: COMMONURL + "api/BidPrice/ChoiceBidPrice?token=" + $.cookie('token'),
                        type: "post",
                        data: {
                            "bid_wl_id": bid_wl_id,
                            "bid_id": bid_id
                        },
                        dataType: "json"
                    }).done(function(redata) {
                    	if(redata.recode=='1003'){
                window.location.href = '/nlogin.html';
                return;
            }
                        if (redata.recode == '0000') {
                            alert(redata.resmessage);
                            window.location.reload();
                        }
                    });
                });
    
        //选标
        $('#selModel').on('show.bs.modal', function(event) {
            var button = $(event.relatedTarget),
                itemid = button.data('itemid');
           // console.log(itemid)
            $.ajax({
                url: COMMONURL + "api/BidPrice/GetPriceList?token=" + $.cookie('token'),
                type: "get",
                data: {
                    bid_id: itemid
                },
                dataType: "json"
            }).done(function(redata) {
                if (redata.recode == '0000') {
                    var data = redata.mod;
                    var compiled = _.template($("#selTpl").html());
                    $("#selModelCon").html(compiled(data));
                }
            });
        });
    },
    	
    	
    	
    abolish: function() {
        //废标
        $('#delModel').on('show.bs.modal', function(event) {
            var button = $(event.relatedTarget),
                itemid = button.data('itemid'),
                bid_no = button.data('bidno');
            $(this).attr({
                'itemid': itemid,
                'bid_no': bid_no
            });
        });

        //废标备注
        $("#delModelSubmit").click(function() {
            var itemid = $('#delModel').attr('itemid'),
                bid_no = $('#delModel').attr('bid_no'),
                reason = $("#reason option:selected").text(),
                remark = $.trim($("#message-text").text());
            $.ajax({
                url: COMMONURL + "api/bidbase/ScrapBid?token=" + $.cookie('token'),
                type: "post",
                data: {
                    bid_id: itemid,
                    bid_no: bid_no,
                    close_reason: reason,
                    reason_info: remark
                }
            }).done(function(redata) {
                if (redata.recode == '0000') {
                    alert('操作成功');
                    $('#delModel').modal('hide');
                    window.location.reload();
                }
            });

        });
    }
}

operator.init();
